# Create your views here.
#-*- encoding: iso8859-1 -*-
import traceback,sys

from datetime import datetime, timedelta

from django.http import HttpResponse, HttpResponseServerError
#from django.views.decorators.csrf import csrf_exempt
from cs_support.models import Ticket
#from copy import deepcopy
#from django.contrib.auth.models import User
#from django.db import connection
from django.utils import simplejson
from django.template import Context,RequestContext
from django.template.loader import get_template

import random
import string

def retornoTicketAjax(request):
    try:
        if request.method == 'POST':
            #user = User.objects.get(pk=int(request.POST['user']))
            email_resposta = request.POST['email_resposta']
            assunto = request.POST['assunto']
            descricao = request.POST['descricao']
            severidade = request.POST['severidade']
            ticket_id = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))
            
            tk = Ticket(email_resposta=email_resposta,
                        assunto=assunto,descricao=descricao,
                        severidade=severidade,ticket_id=ticket_id)            
            tk.save()
            #return HttpResponse(simplejson.dumps({'success':'1'}), mimetype='application/json; charset=UTF-8')
#        else:
#            return HttpResponse(simplejson.dumps({'disable':'1'}), mimetype='application/json; charset=UTF-8')
            
            response_dict = {}
            response_dict['email_resposta'] = email_resposta
            response_dict['ticket_id'] = ticket_id
            
            t = get_template('support_complete.html')
            c = RequestContext(request,response_dict)
            html = t.render(c)
            return HttpResponse(html, mimetype='text/html; charset=UTF-8')            
    except:
        exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
        traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
                          limit=2, file=sys.stdout)
        
def get_csSupport_form_ajax_html(request):
    try:
        #response_dict=get_graph_ativo(ativo_cod=ativo_cod,limit_graph=limit_graph)
        #if response_dict:
        t = get_template('support_form.html')
        c = RequestContext(request)
        html = t.render(c)
        return HttpResponse(html, mimetype='text/html; charset=UTF-8')
        #else:
        #    return HttpResponse(simplejson.dumps('UN'), mimetype='application/json; charset=UTF-8')
    except:
        exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
        traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback,
                          limit=2, file=sys.stdout)         